import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
 

/**
 * 行为模式
 * 采集行为
 * 升级行为
 */ 
export class UpgradeTask<T extends IStateArg<Creep>>  extends State<T>
{

    //
    public  getActions(arg:T):Action[]
    {
        
        const memory = arg.entity.getNodeMemory();
        if(!memory.taskid)
        {
            return [];
        }
        const task =arg.task as ITask<ICreepTaskData<"upgrade">>;
        if(!task)
        {
            // 查找不到任务
            return []
        }
        
        // 建造任务
        // 1.能量不足。则取取能量
        // 1.a sourceid 存在。且能量足够 => withdraw
        // 1.b !(sourceid 存在。且能量足够) => 更新sourceId
        const creep = arg.entity;
      
        if(creep.store.getUsedCapacity(RESOURCE_ENERGY)==0)
        {
            creep.memory.state = 0;

        }
        else   
        {
            creep.memory.state = 1;
        }
        if(creep.memory.state)
        {
            if(creep.ownerRoom().controller)
            {
                return [ActionArgHelper.actionUpgrade(creep,creep.ownerRoom().controller as StructureController)]
            }
            
        }
        else
        {
            const controllerentity = creep.ownerRoom().controller!;
            if(controllerentity && controllerentity.Link() && controllerentity.level ==8)
            {
                creep.memory.sourceId = controllerentity.Link()!.id;
                return [ActionArgHelper.actionWithdraw(creep,controllerentity.Link()!,[{resourceType:RESOURCE_ENERGY}])]
            }
              // 取资源
            if(!creep.memory.sourceId)
            {
                if(controllerentity.level==8)
                {
                    const stores = controllerentity.getWithdrawObjects({resourceType:RESOURCE_ENERGY});
                    if( stores[0])
                    {
                        creep.memory.sourceId = stores[0].id;
                    }
                    else 
                    {
                        const ret = EntityHelper.getWithdrawObject2(creep.ownerRoom(),creep,creep.store.getCapacity()*2);
                        if(ret)
                        {
                            creep.memory.sourceId = ret;
                        }
                       
                    }
                    
                }
                else 
                {
                    if(!controllerentity ||  controllerentity.getContainer().length ==0 )
                    {
                       
                        const sourceid = EntityHelper.getWithdrawObject(creep.ownerRoom(),creep.store.getCapacity()*2);
                        if(sourceid)
                        {
                            creep.memory.sourceId = sourceid;
                        }
                    }
                    else
                    {
                        const container =  controllerentity.getContainer();
                        creep.memory.sourceId=container[0].id;
                        for(const e of container)
                        {
                            if(e.store.getUsedCapacity(RESOURCE_ENERGY)>=creep.store.getCapacity())
                            {
                                creep.memory.sourceId = e.id;
                                break;
                            }
                        }
                    }
                }
            }
            const store =  Game.getObjectById(creep.memory.sourceId as Id<AnyStoreStructure>) ;
            if(store)
            {
                if(store.store.getUsedCapacity(RESOURCE_ENERGY)<creep.store.getCapacity()*2)
                {
                    creep.memory.sourceId = undefined;
                    return [ActionArgHelper.actionMoveto(creep,{pos:store.pos,range:3},{})]
                }
                if(store.structureType == STRUCTURE_SPAWN )
                {
                    const containers = creep.getEntity("SpawnEntity").getContainer();
                    if(containers.length>0)
                    {
                        creep.memory.sourceId = undefined;
                    }
                }
                return [ActionArgHelper.actionWithdraw(creep,store,[{resourceType:RESOURCE_ENERGY}])]
            }
            else
            {
                const spawn = arg.entity.getEntity("SpawnEntity");
                const res = spawn.getPickUpResource();
                if(res)
                {
                    return [ActionArgHelper.actionPickup(creep, res)]
                }
            }
          
        }
        // 能量足够。则进行工作
        /**
         * targetId 不存在 -> targetId
         * targetId  存在 -> 进行工作
        */
        return [ ];
    }
}
